<?php

class Management
{
    private $apiurl;
    private $apis;
    private $action;
    private $start = 0;
    private $length = 20;

    public function __construct($apiurl) {
        $this->apiurl   = $apiurl;
        $this->apis     = array(
            'gagList','removeShutUp','forbidLogin'
        );
    }

    public function execute ($action) {
        if (empty($action)) return false;
        if (!in_array($action, $this->apis)) return false;

        $this->action = $action;
        $result = $this->$action(request());
        return $result;
    }

    /**
     * 禁言列表
     * @param $req
     * @return string
     */
    private function gagList($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $res = $res['gag_list'];
        $req['uid'] = 0;
        if ($res) $req['uid'] = $res[count($res) -1]['uid'];
        $pageUrl = $this->pageUrl($req);
        $shutUpMsg = $this->getShutUpMsg();
        return render('gagList', array('list' => $res, 'shutUpMsg'=> $shutUpMsg, 'uid' => $req['uid'], 'pageUrl' => $pageUrl));
    }


    /**
     * 封禁用户登录信息
     * @param $req
     * @return string
     */
    private function forbidLogin($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        $res['login'] = 1;
        $req['id'] = 0;
        if ($res) $req['id'] = $res['forbid_list'][count($res['forbid_list']) -1]['id'];
        $pageUrl = $this->pageUrl($req);
        return render('forbidLoginList', array('list' => $res, 'pageUrl' => $pageUrl));
    }


    /**
     * 删除禁言
     * @param $req
     * @return string
     */
    private function removeShutUp($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (!$res) {
            echo 0;
        }else{
            echo 1;
        }
        exit();
    }

    function getShutUpMsg(){
        $file = '/tmp/forbiddenInfo/shutUpMsg.txt';
        $a = file_get_contents($file);
        $b = json_decode($a,true);
        return $b;
    }

    /**
     * 传参过滤
     * @param unknown $req
     * @return multitype:string number NULL
     */
    private function filterArgumentsOfPassed($req) {
        switch ($this->action){
            case 'gagList':
                $param = array(
                    'do'     => 'getShutUpList',
                    'format' => 'json',
                    'start'  => empty($req['start']) ? $this->start : intval($req['start']),
                    'length' => empty($req['length']) ? $this->length : intval($req['length']),
                    'uid'    => isset($req['uid']) ? $req['uid'] : 0 ,
                    'id'     => empty($req['uid']) ? 0 : $req['uid']
                );
                break;
            case 'removeShutUp':
                $param = array(
                    'do'     => 'removeShutUp',
                    'format' => 'json',
                    'uid'    => intval($req['uid']),
                );
                break;
            case 'forbidLogin':
                $param = array(
                    'do'     => 'getForbidLoginList',
                    'format' => 'json',
                    'start'  => empty($req['start']) ? $this->start : intval($req['start']),
                    'length' => empty($req['length']) ? $this->length : $req['length'],
                );
                break;
        }

        return $param;
    }

    /**
     * 分页数据处理
     * @param unknown $req
     * @return multitype:string unknown
     */
    private function pageUrl ($req) {
        $mpurl = "admin.php?ac=".$req['ac']."&op=".$req['op'];
        switch ($req['op']){
            case "gagList":
                $mpurl .= "&uid={$req['uid']}";
                $pageurl = multi($req['start'], $this->length, $mpurl);
                break;
            case "forbidLogin":
                $mpurl .= "&id={$req['id']}";
                $pageurl = multi($req['start'], $this->length, $mpurl);
                break;
        }

        return $pageurl;
    }

}

$req = request();
$diaryTopic   = new Management($_SC ['apiurl']);
echo $diaryTopic->execute($req['op']);

